package com.sentiment.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.bson.Document;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.sentiment.bean.DailyReportMini;
import com.sentiment.bean.OperationInfo;
import com.sentiment.config.Config;
import com.sentiment.database.bean.CrawlInfo;
import com.sentiment.database.bean.DailyReport;
import com.sentiment.database.dbUtil.DbUtil;
import com.sentiment.entrance.EnterToKmp;
import com.sentiment.mail.SendMailOnTime;
import com.sentiment.port.EnterTo;
import com.sentiment.report.DailyReportUtils;
import com.sentiment.tools.Format;

@WebServlet("/MailServlet")
public class MailServlet extends HttpServlet{
	private static final  long serialVersionUID = 1L;
	
	private static final Logger LOG = LoggerFactory.getLogger(MailServlet.class);
	
	private DbUtil dbu = new DbUtil();
	
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException {
		String methodName = request.getParameter("method");
		try {
			Method method = getClass().getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
			method.invoke(this, request, response);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}		
	}
	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	
	public void getMailList(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException{
		List<String> toMail = new ArrayList<String>();
		String[] string = request.getParameter("toMail").split(";");
		for(int i = 0; i < string.length; i++){
			toMail.add(string[i]);
		}	
		String subject = request.getParameter("subject");
		String content = request.getParameter("content");
		SendMailOnTime.sendMail(toMail, subject, content);
		String jsonStr = "{\"info\":" + "\"" + OperationInfo.OperSuccess + "\"" +"}";

		System.out.println(jsonStr);
		
		response.setCharacterEncoding("UTF-8");
		response.getWriter().print(jsonStr);

	}
}
